package edu.auctionmart

import org.joda.time.*
import org.jadira.usertype.dateandtime.joda.*

class Feedback {
	
	User leftBy
	User leftFor
	Auction auction
	Integer availability
	Integer accuracy
	Integer communication
	Integer overallRating
	String comment
	DateTime dateCreated
	String formattedDateCreated
	
	static beforeInsert = {
		dateCreated = new DateTime().now()
		
		String year = dateCreated.getYear()
		String month = dateCreated.getMonthOfYear()
		String date = dateCreated.getDayOfMonth()
		formattedDateCreated = month + "-" + date + "-" + year
	}
		
    static constraints = {
		leftBy(nullable: false)
		leftFor(validator: {leftFor, feedback -> 
			if (feedback.leftBy != null) 
				leftFor.getUsername() != feedback.leftBy.getUsername()
			}, nullable: false)
		auction(nullable: false)
		availability(validator: { 
			return ((it == null) || (it >= 1 && it <=5))
		}, nullable: true)
		accuracy(validator: { 
			return ((it == null) || (it >= 1 && it <=5))
		}, nullable: true)
		communication(validator: { 
			return ((it == null) || (it >= 1 && it <=5))
		}, nullable: true)
		overallRating(validator: { 
			return ((it == null) || (it >= 1 && it <=5))
		}, nullable: true)
		comment(size:1..150, nullable: true, validator: { return it == null || it.trim().length() > 0})
		formattedDateCreated(nullable: true)
    }
	


}
